const app = getApp();
Page({
  data: {
    baseUrl: app.globalData.baseUrl,
    fileUrl: app.globalData.fileUrl,
    searchParam:{},
    sapId: '',
    staffId: '',
    cardList: [],
    courseIndex: null,
    projectType:0,
    isKeJianShow:false,
  },
  loadData: function () {
    var that = this;
    wx.showLoading({
      title: '数据加载中', // 提示的内容  
      mask: true, // 是否显示透明蒙层，防止触摸穿透  
    });
    wx.request({
      url: that.data.baseUrl + '/v1/openApi/staff/myExamine',
      method: 'POST',
      data: {
        projectType:that.data.projectType,
        sapId: that.data.sapId,
        staffId: that.data.staffId,
        ...that.data.searchParam
      },
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        wx.hideLoading();
        var data = res.data.rows;
        
      
          that.setData({
            cardList: data
          })
        
      }, fail(res) {
        wx.hideLoading();
        wx.showToast({
          title: '我的考试查询失败，请联系管理人员！',
          icon: 'none',
          duration: 3000
        });
      }
    });
  },
  onLoad: function (params) {
    const initParam = this.selectComponent("#searchTab").data
    this.setData({
      searchParam: {
        endTime:initParam.currentTime[1],
        startTime:initParam.currentTime[0],
        sign:initParam.signStatus
      },
      projectType:params.type,
      sapId: wx.getStorageSync('userinfo').siocSapNo,
      staffId: wx.getStorageSync('userinfo').staffId
    })
    this.loadData();
  },

  showKeJian(card){
    const that = this
    wx.navigateTo({
      url: `/pages/file-list/file-list?id=${card.target.dataset.item.trainManagerTemple.id}`,
    });
    // this.setData({
    //   isKeJianShow:!this.data.isKeJianShow
    // })
  },

  uploadImages: function (event) {
    let index = event.currentTarget.dataset.index;
    let that = this;
    wx.chooseMedia({
      count: 9, // 默认9  
      mediaType: ['image'],
      sourceType: ['camera'],
      camera: 'back', // 使用后置摄像头  
      success: function (res) {
        var path = res.tempFiles[0].tempFilePath;
        wx.showLoading({
          title: '签到图片上传中', // 提示的内容  
          mask: true, // 是否显示透明蒙层，防止触摸穿透  
        });
        wx.uploadFile({
          url: that.data.fileUrl + '/common/picUpload',
          filePath: path,
          name: 'file',
          success(res) {
            let fileInfo = JSON.parse(res.data);
            let fileName = fileInfo.fileName;
            var list = that.data.cardList;
            if (!list[index].imageList) {
              list[index].images = [];
              list[index].imageList = [];
            }
            list[index].images.push(fileName);
            list[index].imageList.push(path);
            that.setData({
              cardList: list
            });
            wx.hideLoading();
          },
          fail(err) {
            wx.hideLoading();
            wx.showToast({
              title: "上传失败",
              icon: 'none',
              duration: 3000
            });
          }
        });
      },
      fail: function (res) {
        wx.showToast({
          title: "签到失败",
          icon: 'none',
          duration: 3000
        });
      }
    });

    // this.saveSignnature(this,item);
  },

 

  //搜索返回
  doSearch(res){

    let newParams = {
      endTime:res.detail.currentTime[1],
      startTime:res.detail.currentTime[0],
      sign:Boolean(+res.detail.signStatus),
      pass:Boolean(+res.detail.achievementType)
    }
    //全部要删去参数
    if(res.detail.achievementType=="2"){
      delete newParams.pass
    }
    if(res.detail.signStatus=="2"){
      delete newParams.sign
    }

    this.setData({
      searchParam: newParams
    })

    this.loadData();
  },

  staffSignIn: function (event) {

    let item = event.currentTarget.dataset.item;
    const imageList = this.selectComponent(`#imageUp${event.currentTarget.dataset.index}`).data.imageList

    if (imageList && imageList.length > 0) {
      this.saveSignnature(this, item, imageList);
    } else {
      wx.showToast({
        title: "请先上传现场图片",
        icon: 'none',
        duration: 3000
      });
    }
  },
  saveSignnature: function (that, item, imageList) {

    const tempArray = [];
    imageList.forEach(item=>{
      tempArray.push(item.url);
    })

    let data = {
      sapId: that.data.sapId,
      trainId: item.id,
      type: 2,//员工签到
      images: tempArray,
      staffId: that.data.staffId
    };
    wx.showLoading({
      title: '签到中', // 提示的内容  
      mask: true, // 是否显示透明蒙层，防止触摸穿透  
    });
    wx.request({  //todolzl
      url: that.data.baseUrl + '/v1/openApi/sign',
      method: 'POST',
      data: data,
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        wx.hideLoading();
        if (res.data.code != 200) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 3000
          });
          return;
        }
        that.loadData();
      }, fail(res) {
        wx.hideLoading();
        wx.showToast({
          title: '签名保存失败，请联系管理人员！',
          icon: 'none',
          duration: 3000
        });
      }
    });
  },
  startExam: function (event) {
  },
  coursebindChange: function (event) {
    var item = event.currentTarget.dataset.item;
    var index = event.detail.value;
    var key = item.courseList[index].key;


    wx.redirectTo({
      url: `/pages/my-exma/my-exma?trainId=${item.id}&courseId=${key}&projectType=${this.data.projectType}&sapId=${this.data.sapId}`
    })
  }
})